package com.nantong.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.nantong.admin.annotation.PermissionRegister;
import com.nantong.admin.domain.model.request.quote.QuoteCreateRequest;
import com.nantong.admin.domain.model.request.quote.QuoteSearch;
import com.nantong.admin.domain.model.response.quote.QuoteVo;
import com.nantong.admin.service.IQuoteService;
import com.nantong.common.core.enums.commonEnums.QuoteStatusEnum;
import com.nantong.common.mybatis.core.page.PageQuery;
import com.nantong.common.mybatis.core.page.TableDataInfo;
import com.nantong.common.core.domain.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 报价单Controller
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system/quote")
@PermissionRegister(
    name = "报价单管理",
    key = "system:quote",
    type = 2,  // 2=菜单
    path = "quote",
    component = "/views/Quote/QuoteView.vue",
    icon = "file-text",
    sortOrder = 4,
    parentKey = "system"  // 父菜单是system
)
@Tag(name = "报价单管理", description = "报价单管理相关接口")
public class QuoteController {

    private final IQuoteService quoteService;

    /**
     * 获取报价单列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取报价单列表", description = "获取系统报价单列表")
    @SaCheckPermission("system:quote:list")
    @PermissionRegister(
            name = "报价单列表查询",
            key = "system:quote:list",
            type = 3,  // 3=按钮
            remark = "查询报价单数据列表"
    )
    public R<List<QuoteVo>> list(QuoteSearch quoteSearch) {
        List<QuoteVo> quotes = quoteService.searchList(quoteSearch);
        return R.ok(quotes);
    }

    /**
     * 获取报价单分页
     */
    @SaCheckPermission("system:quote:page")
    @GetMapping("/page")
    @Operation(summary = "报价单分页查询", description = "获取系统报价单分页数据")
    @PermissionRegister(
            name = "报价单分页查询",
            key = "system:quote:page",
            type = 3,  // 3=按钮
            remark = "查询报价单数据分页"
    )
    public TableDataInfo<QuoteVo> page(QuoteSearch quoteSearch, PageQuery pageQuery) {
        return quoteService.selectPageList(quoteSearch, pageQuery);
    }

    /**
     * 获取报价单详细信息
     */
    @GetMapping("/{quoteId}")
    @Operation(summary = "获取报价单详情", description = "根据报价单ID获取报价单详情")
    @SaCheckPermission("system:quote:query")
    @PermissionRegister(
            name = "报价单详情查询",
            key = "system:quote:query",
            type = 3,  // 3=按钮
            remark = "查询报价单数据详情"
    )
    public R<QuoteVo> getInfo(@PathVariable Long quoteId) {
        QuoteVo quote = quoteService.selectQuoteById(quoteId);
        return R.ok(quote);
    }

    /**
     * 新增报价单
     */
    @PostMapping
    @Operation(summary = "新增报价单", description = "新增系统报价单")
    @SaCheckPermission("system:quote:add")
    @PermissionRegister(
            name = "报价单新增",
            key = "system:quote:add",
            type = 3,  // 3=按钮
            remark = "新增报价单数据"
    )
    public R<Void> add(@RequestBody QuoteCreateRequest quoteCreateRequest) {
        quoteService.insertQuote(quoteCreateRequest);
        return R.ok();
    }

    /**
     * 修改报价单
     */
    @PutMapping
    @Operation(summary = "修改报价单", description = "修改系统报价单")
    @SaCheckPermission("system:quote:edit")
    @PermissionRegister(
            name = "报价单修改",
            key = "system:quote:edit",
            type = 3,  // 3=按钮
            remark = "修改报价单数据"
    )
    public R<Void> edit(@RequestBody QuoteCreateRequest quoteCreateRequest) {
        quoteService.updateQuote(quoteCreateRequest);
        return R.ok();
    }

    /**
     * 删除报价单
     */
    @DeleteMapping("/{quoteId}")
    @Operation(summary = "删除报价单", description = "删除系统报价单")
    @SaCheckPermission("system:quote:delete")
    @PermissionRegister(
            name = "报价单删除",
            key = "system:quote:delete",
            type = 3,  // 3=按钮
            remark = "删除报价单数据"
    )
    public R<Void> remove(@PathVariable Long quoteId) {
        quoteService.deleteQuoteById(quoteId);
        return R.ok();
    }

    /**
     * 根据客户ID获取报价单列表
     */
    @GetMapping("/customer/{customerId}")
    @Operation(summary = "根据客户ID获取报价单列表", description = "获取指定客户的报价单列表")
    @SaCheckPermission("system:quote:customer")
    @PermissionRegister(
            name = "客户报价单查询",
            key = "system:quote:customer",
            type = 3,  // 3=按钮
            remark = "查询指定客户的报价单列表"
    )
    public R<List<QuoteVo>> getQuotesByCustomerId(@PathVariable Long customerId) {
        List<QuoteVo> quotes = quoteService.selectQuotesByCustomerId(customerId);
        return R.ok(quotes);
    }

    /**
     * 提交报价单
     */
    @PutMapping("/submit/{quoteId}")
    @Operation(summary = "提交报价单", description = "提交报价单进行审核")
    @SaCheckPermission("system:quote:submit")
    @PermissionRegister(
            name = "报价单提交",
            key = "system:quote:submit",
            type = 3,  // 3=按钮
            remark = "提交报价单进行审核"
    )
    public R<Void> submitQuote(@PathVariable Long quoteId) {
        quoteService.updateQuoteStatus(quoteId, QuoteStatusEnum.PENDING_REVIEW.getCode()); // 待审核状态
        return R.ok();
    }

    /**
     * 接收报价单
     */
    @PutMapping("/receive/{quoteId}")
    @Operation(summary = "接收报价单", description = "接收报价单")
    @SaCheckPermission("system:quote:receive")
    @PermissionRegister(
            name = "报价单接收",
            key = "system:quote:receive",
            type = 3,  // 3=按钮
            remark = "接收报价单"
    )
    public R<Void> receiveQuote(@PathVariable Long quoteId) {
        quoteService.updateQuoteStatus(quoteId, QuoteStatusEnum.RECEIVED.getCode()); // 已接收状态
        return R.ok();
    }

    /**
     * 拒绝报价单
     */
    @PutMapping("/reject/{quoteId}")
    @Operation(summary = "拒绝报价单", description = "拒绝报价单")
    @SaCheckPermission("system:quote:reject")
    @PermissionRegister(
            name = "报价单拒绝",
            key = "system:quote:reject",
            type = 3,  // 3=按钮
            remark = "拒绝报价单"
    )
    public R<Void> rejectQuote(@PathVariable Long quoteId) {
        quoteService.updateQuoteStatus(quoteId, QuoteStatusEnum.REJECTED.getCode()); // 已拒绝状态
        return R.ok();
    }

}